// 状态和事件
typedef enum { IDLE, RUNNING, ERROR } State;
typedef enum { START, STOP, ERROR_EVENT } Event;

// 表结构
typedef struct {
    State current;  // 当前状态
    Event event;    // 触发事件
    State next;     // 下一状态
    void (*action)(void); // 执行的操作
} Transition;

// 状态转移表
Transition transitions[] = {
    {IDLE, START, RUNNING, start_running},
    {RUNNING, STOP, IDLE, stop_running},
    {RUNNING, ERROR_EVENT, ERROR, handle_error},
    // 加新状态？在这加一行
};

State current_state = IDLE;

void process_event(Event event) {
    int size = sizeof(transitions) / sizeof(Transition);
    for (int i = 0; i < size; i++) {
        if (transitions[i].current == current_state && transitions[i].event == event) {
            current_state = transitions[i].next;
            if (transitions[i].action) transitions[i].action();
            return;
        }
    }
    invalid_transition(); // 没找到就报个错
}